package com.chief;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Transactional
public class TestAPIServiceImpl implements TestAPIService {


    @Qualifier("testServiceImpl")
    @Autowired
    private TestService testService;

    @Autowired(required = false)
    private UserMapper userMapper;


    @Override
    @TargetDataSource("db1")
    public String testAPI() {
        this.userMapper.saveUser();
        List<User> allUser = this.userMapper.getAllUser();
        for (User user : allUser) {
            System.out.println("testAPI    "+user.toString());
        }

        this.userMapper.updateUser();
        allUser = this.userMapper.getAllUser();
        for (User user : allUser) {
            System.out.println("testAPI upd    "+user.toString());
        }

        this.userMapper.deleteUser();
        allUser = this.userMapper.getAllUser();
        for (User user : allUser) {
            System.out.println("testAPI  delete  "+user.toString());
        }


        return "testAPI--->" +
                this.testService.test()+",db1==="+this.userMapper.getUser().toString();
    }


    @Override
    @TargetDataSource("db2")
    public String testAPI2() {

        this.userMapper.saveUser();
        List<User> allUser = this.userMapper.getAllUser();
        for (User user : allUser) {
            System.out.println("testAPI2    "+user.toString());
        }

        this.userMapper.updateUser();
        allUser = this.userMapper.getAllUser();
        for (User user : allUser) {
            System.out.println("testAPI2 upd    "+user.toString());
        }

        this.userMapper.deleteUser();
        allUser = this.userMapper.getAllUser();
        for (User user : allUser) {
            System.out.println("testAPI2  delete  "+user.toString());
        }

        return "testAPI2--->" + this.testService.test()+",db1==="+
                this.userMapper.getUser().toString() ;
    }
}
